package com.hl.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.domain.Article;
import com.hl.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Hr
 * @date 2024/9/5 20:48
 */
@Service
public class ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    //查询所有
    public Map<String,Article> findAllArticle(Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Article> articleList = articleMapper.findAllArticle(pageNum, pageSize);
        PageInfo<Article> pageInfo = new PageInfo<>(articleList);
        Map map = new HashMap<>();
        map.put("list",articleList);
        map.put("total",pageInfo.getTotal());
        return map;
    }

    //新增
    public int insertSelective(Article record){
        return  articleMapper.insertSelective(record);
    }

    //修改
   public int updateByPrimaryKeySelective(Article record){
        return articleMapper.updateByPrimaryKeySelective(record);
   }

    //删除
    public int deleteByPrimaryKey(Long id){
        return articleMapper.deleteByPrimaryKey(id);
    }

    //条件查询
    public Map<String,Article> findAllByLike(String articlename, String typeName, Date startTime, Date endTime,Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Article> articleList = articleMapper.findAllByLike(articlename,typeName,startTime,endTime);
        PageInfo<Article> pageInfo = new PageInfo<>(articleList);

        Map map = new HashMap<>();
        map.put("list",articleList);
        map.put("total",pageInfo.getTotal());
        return map;
    }

}
